<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将一个数组分割成多个</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.array-change-key-case.html">« array_change_key_case</a></li>
      <li style="float: right;"><a href="function.array-column.html">array_column »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.array.html">数组 函数</a></li>
    <li>将一个数组分割成多个</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.array-chunk" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_chunk</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.2.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_chunk</span> &mdash; <span class="dc-title">将一个数组分割成多个</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-chunk-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_chunk</strong></span>(<span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$preserve_keys</code><span class="initializer"> = <strong><code>false</code></strong></span></span>): <span class="type">array</span></div>

    <p class="para rdfs-comment">
     将一个数组分割成多个数组，其中每个数组的单元数目由
     <code class="parameter">length</code> 决定。最后一个数组的单元数目可能会少于 <code class="parameter">length</code> 个。
     
    </p>
   </div>


 <div class="refsect1 parameters" id="refsect1-function.array-chunk-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">array</code></dt>

     <dd>

      <p class="para">
       需要操作的数组
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       每个数组的单元数目
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">preserve_keys</code></dt>

     <dd>

      <p class="para">
       设为 <strong><code>true</code></strong>，可以使 PHP
     保留输入数组中原来的键名。如果你指定了 <strong><code>false</code></strong>，那每个结果数组将用从零开始的新数字索引。默认值是 <strong><code>false</code></strong>。
      </p>
     </dd>

    

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-chunk-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   得到的数组是一个多维数组中的单元，其索引从零开始，每一维包含了 <code class="parameter">length</code> 个元素。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.array-chunk-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果 <code class="parameter">length</code> 小于 <code class="literal">1</code>，会抛出 <span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-chunk-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       如果 <code class="parameter">length</code> 小于 <code class="literal">1</code>，现在会抛出
       <span class="classname"><a href="class.valueerror.html" class="classname">ValueError</a></span>；之前会引发 <strong><code>E_WARNING</code></strong> 
       级别的错误且函数会返回 <strong><code>null</code></strong>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.array-chunk-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4894">
    <p><strong>示例 #1 <span class="function"><strong>array_chunk()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input_array&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'c'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'d'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'e'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_chunk</span><span style="color: #007700">(</span><span style="color: #0000BB">$input_array</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_chunk</span><span style="color: #007700">(</span><span style="color: #0000BB">$input_array</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [0] =&gt; a
            [1] =&gt; b
        )

    [1] =&gt; Array
        (
            [0] =&gt; c
            [1] =&gt; d
        )

    [2] =&gt; Array
        (
            [0] =&gt; e
        )

)
Array
(
    [0] =&gt; Array
        (
            [0] =&gt; a
            [1] =&gt; b
        )

    [1] =&gt; Array
        (
            [2] =&gt; c
            [3] =&gt; d
        )

    [2] =&gt; Array
        (
            [4] =&gt; e
        )

)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-chunk-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-slice.html" class="function" rel="rdfs-seeAlso">array_slice()</a> - 从数组中取出一段</span></li>
   </ul>
  </p>
 </div>

  </div></div></div></body></html>